/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.support.ohos.designlibdemo.provider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.support.ohos.designlibdemo.DetailAbility;
import com.support.ohos.designlibdemo.ResourceTable;
import com.support.ohos.designlibdemo.bean.ListItemBean;
import com.support.ohos.designlibdemo.pref.DisplayMode;
import com.support.ohos.designlibdemo.utils.Cheeses;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;

import java.util.List;

/**
 * ListProvider列表适配器
 */
public class ListProvider extends BaseItemProvider {
    private final Ability ability;
    private final List<ListItemBean> data;

    public ListProvider(Ability ability, List<ListItemBean> values) {
        this.ability = ability;
        this.data = values;
    }

    @Override
    public int getCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int pos) {
        return data.get(pos);
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public Component getComponent(int pos, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        if (component == null) {
            cpt = LayoutScatter.getInstance(ability.getContext())
                    .parse(ResourceTable.Layout_layout_main_provider_list_item, null, false);
        } else {
            cpt = component;
        }
        boolean night = DisplayMode.isNight();
        Component t1 = cpt.findComponentById(ResourceTable.Id_avatar);
        if (t1 instanceof Image) {
            Image image = (Image) t1;
            Glide.with(ability.getContext())
                    .load(data.get(pos).getAvatar())
                    .apply(RequestOptions.circleCropTransform())
                    .into(image);
        }
        Component t2 = cpt.findComponentById(ResourceTable.Id_text1);
        if (t2 instanceof Text) {
            Text text = (Text) t2;
            text.setText(data.get(pos).getText());
            text.setTextColor(new Color(Cheeses.getColorVersion4(ability, night ?
                    ResourceTable.Color_white : ResourceTable.Color_black)));
        }
        Cheeses.setBgColor(ability.getContext(), cpt, night ?
                ResourceTable.Color_tabNorBgNight : ResourceTable.Color_white);
        cpt.setClickedListener(component1 -> {
            Intent intent = new Intent();
            intent.setParam(Cheeses.EXTRA_NAME, data.get(pos).getText());
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(ability.getContext().getBundleName())
                    .withAbilityName(DetailAbility.class.getName())
                    .build();
            intent.setOperation(operation);
            ability.startAbility(intent);
        });
        return cpt;
    }
}